
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Core.GameObjects
{
    public abstract class DrawableGameObject : GameObject, iDrawable
    {

        public Activity Activity { get; protected set; }
        public Rotation Rotation { get; protected set; }
        public Color DrawColor { get; protected set; }

        public DrawableGameObject(int id, string name) : base(id, name)
        {
            Activity = Activity.NotMoving;
            Rotation = Rotation.Up;
            DrawColor = Color.White;
        }

        public virtual void RotateObject(Rotation NewRotation)
        {
            if (NewRotation != Rotation)
            {
                Rotation = NewRotation;
                //Set new animation

                //Rotate all child objects to match
                foreach (GameObject go in _ChildObjects)
                {
                    if (go is DrawableGameObject)
                    {
                        DrawableGameObject t = (DrawableGameObject)go;
                        t.RotateObject(NewRotation);
                    }
                }
            }
        }

        public virtual void SetActivity(Activity NewActivity)
        {
            if (Activity != NewActivity)
            {
                //Update Activity
                Activity = NewActivity;

                switch (Activity)
                {
                    case Activity.Moving:
                        //Animation.RunAnimation = true;
                        break;

                    case Activity.NotMoving:
                        //Animation.RunAnimation = false;
                        break;
                }
                //For each child object
                foreach (GameObject go in _ChildObjects)
                {
                    //IF child object is drawable then do it.
                    if (go is DrawableGameObject)
                    {
                        DrawableGameObject t = (DrawableGameObject)go;

                        t.SetActivity(Activity);
                    }
                }
            }
        }

        public void SetDrawColor(Color Color)
        {
            DrawColor = Color;
        }

        public override void Update(GameTime gameTime)
        {
            //Get time elapsed as float
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            
            base.Update(gameTime);
        }

        public virtual void Draw(SpriteBatch spriteBatch, Point destPoint)
        {
            //Rectangle destRectangle = new Rectangle(destPoint.X, destPoint.Y, _TileSet.TileWidth, _TileSet.TileHeight);

            //int SpriteID = GetFirstAnimationTile() + _Animation.TileAnimationFrame;

            //int tileY = (SpriteID / _TileSet.TilesPerRow);
            //int tileX = (SpriteID % _TileSet.TilesPerRow);
            //Rectangle rectSource = new Rectangle(
            //    (tileX * _TileSet.TileWidth) + _TileSet.HorizontalSpacing * tileX,
            //    (tileY * _TileSet.TileHeight) + _TileSet.VerticalSpacing * tileY,
            //    _TileSet.TileWidth, _TileSet.TileHeight);

            //spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            //spriteBatch.Draw(_TileSet.TileSetTexture, destRectangle, rectSource, _DrawColor);
            //spriteBatch.End();

            foreach (GameObject go in _ChildObjects)
            {
                if (go is iDrawable)
                {
                    iDrawable t = (iDrawable)go;

                    t.Draw(spriteBatch, destPoint);
                }
            }
        }
    }
}
